jQuery(function($) {
    var startTime = new Date();
    var viewedProducts = [];
    
    // Track product views
    $(document).on('click', '.product, .add-to-cart', function() {
        var productId = $(this).data('product-id');
        if (productId && !viewedProducts.includes(productId)) {
            viewedProducts.push(productId);
            $.post(bdaTracker.ajax_url, {
                action: 'bda_track_product_view',
                product_id: productId,
                nonce: bdaTracker.nonce
            });
        }
    });
    
    // Track page view duration
    $(window).on('beforeunload', function() {
        var duration = Math.round((new Date() - startTime) / 1000);
        $.ajax({
            url: bdaTracker.ajax_url,
            type: 'POST',
            async: false,
            data: {
                action: 'bda_track_page_view',
                duration: duration,
                nonce: bdaTracker.nonce
            }
        });
    });
});